package cn.threewaters.qtyy.web.controller.sys;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dit.one.entity.SysFunction;
import cn.threewaters.qtyy.service.sys.SysFunctionService;
import cn.threewaters.qtyy.util.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "菜单管理")
@Controller
@RequestMapping(value = "/sysFunction")
public class SysFunctionController {
	@Resource
	private SysFunctionService sysFunctionService;

	@ApiIgnore
	@ApiOperation(value = "菜单页面")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public ModelAndView sysFunMain() {
		ModelAndView modelAndView = new ModelAndView("system/sys_function/sys_function_main");
		return modelAndView;
	}

	@ApiIgnore
	@ApiOperation(value = "菜单增加页面")
	@RequestMapping(value = "/add/view", method = RequestMethod.GET)
	public ModelAndView sysFunctionAddView(String funId, ModelMap modelMap) {
		modelMap.addAttribute("parentId", funId);
		ModelAndView modelAndView = new ModelAndView("system/sys_function/sys_function_add");
		return modelAndView;
	}

	@ApiIgnore
	@ApiOperation(value = "菜单修改页面")
	@RequestMapping(value = "/update/view", method = RequestMethod.GET)
	public ModelAndView sysFunctionUpdateView(String funId, ModelMap modelMap) throws Exception {
		modelMap.addAttribute("sysFunction", sysFunctionService.selectById(funId));
		ModelAndView modelAndView = new ModelAndView("system/sys_function/sys_function_update", modelMap);
		return modelAndView;
	}

	@ApiOperation(value = "菜单树形列表查询")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson sysFunctionJsonData() throws Exception {
		List<Map<String, String>> functionZtreeData = sysFunctionService.getZtreeData();
		return ResponseJson.builder().success(true).msg("查询成功").obj(functionZtreeData).build();
	}

	@ApiOperation(value = "菜单信息查询")
	@RequestMapping(value = "/info/{funId}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getSysFunctionInfo(@PathVariable(value = "funId") String funId) throws Exception {
		SysFunction sysFunction = sysFunctionService.selectById(funId);
		return ResponseJson.builder().success(true).msg("查询成功").obj(sysFunction).build();
	}

	@ApiOperation(value = "菜单增加")
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson addSysFunction(SysFunction sysFunction) throws Exception {
		sysFunctionService.insert(sysFunction);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", sysFunction.getId());
		jsonObject.put("pId", sysFunction.getParentId());
		jsonObject.put("name", sysFunction.getFunName());
		return ResponseJson.builder().success(true).msg("增加成功").obj(jsonObject).build();
	}

	@ApiOperation(value = "菜单修改")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson updateSysFunction(SysFunction sysFunction) throws Exception {
		sysFunctionService.updateById(sysFunction);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", sysFunction.getId());
		jsonObject.put("pId", sysFunction.getParentId());
		jsonObject.put("name", sysFunction.getFunName());
		return ResponseJson.builder().success(true).msg("修改成功").obj(jsonObject).build();
	}

	@ApiOperation(value = "菜单删除")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson deleteFun(@RequestBody List<String> idList) throws Exception {
		sysFunctionService.deleteBatchIds(idList);
		return ResponseJson.builder().success(true).msg("菜单删除成功").build();
	}
}
